//
//  MJRefreshImageHeader.m
//  MJRefreshExample
//
//  Created by MJ Lee on 15/4/24.
//  Copyright (c) 2015年 Jagtu. All rights reserved.
//

#import "MJRefreshImageHeader.h"

@interface MJRefreshImageHeader()
{
    __unsafe_unretained UIImageView *_arrowView;
}
@end

@implementation MJRefreshImageHeader

-(id)init{
    self = [super init];
    if (self) {
        //自定义设置
        self.lastUpdatedTimeLabel.hidden = YES;
        self.stateLabel.hidden = YES;
    }
    return self;
}

#pragma mark - 懒加载子控件
- (UIImageView *)arrowView
{
    if (!_arrowView) {
//        UIImage *image = [UIImage imageNamed:MJRefreshSrcName(@"update.png")] ?: [UIImage imageNamed:MJRefreshFrameworkSrcName(@"update.png")];
//edit by zxl 20170801
//        UIImage *image = [UIImage imageNamed:MJRefreshSrcName(@"jkt_logo.png")] ?: [UIImage imageNamed:MJRefreshFrameworkSrcName(@"jkt_logo.png")];
        
        UIImage *image = [UIImage imageNamed:@"jkt_logo"];
//end
        UIImageView *arrowView = [[UIImageView alloc] initWithImage:image];
        [self addSubview:_arrowView = arrowView];
    }
    return _arrowView;
}


#pragma makr - 重写父类的方法
- (void)prepare
{
    [super prepare];
    
    
}

- (void)placeSubviews
{
    [super placeSubviews];
    
    // 箭头的中心点
    CGFloat arrowCenterX = self.mj_w * 0.5;
    if (!self.stateLabel.hidden) {
        arrowCenterX -= 80;
    }
    CGFloat arrowCenterY = self.mj_h * 0.5;
    CGPoint arrowCenter = CGPointMake(arrowCenterX, arrowCenterY);
    
    // 箭头
    if (self.arrowView.constraints.count == 0) {
        self.arrowView.mj_size = self.arrowView.image.size;
        self.arrowView.center = arrowCenter;
    }
}

- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState
    
    // 根据状态做事情
    if (state == MJRefreshStateIdle) {
        [self stopAnimating];
    } else if (state == MJRefreshStatePulling) {
        
        [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
            self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
        }];
    } else if (state == MJRefreshStateRefreshing) {
        [self startAnimating];
    }
}

-(void)startAnimating
{
//    [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
//        self.arrowView.transform = CGAffineTransformIdentity;
//    } completion:^(BOOL finished) {
        CABasicAnimation* rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.toValue = [NSNumber numberWithFloat: -M_PI * 2.0 ];
        rotationAnimation.duration = MJRefreshSlowAnimationDuration*1.5;
        rotationAnimation.cumulative = YES;
        rotationAnimation.repeatCount = INT16_MAX;
        [_arrowView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
//    }];
}

-(void)stopAnimating
{
    [self.arrowView.layer removeAllAnimations];
    [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
        self.arrowView.transform = CGAffineTransformIdentity;
    }];
}

@end
